import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_bloc_todo/model/todo_model.dart';
import 'package:flutter_bloc_todo/view/todo/bloc/todo_cubit.dart';

class TodoPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          ToDoModel toDoModel = new ToDoModel();
          toDoModel.title = "标题";
          toDoModel.content= "内容";
          toDoModel.leave0l="1";
          toDoModel.updateAt = DateTime.now();
          BlocProvider.of<TodoCubit>(context).saveTodo(toDoModel);
        },
      ),
      appBar: AppBar(
        title: Text("测试"),
      ),
      body: SingleChildScrollView(
        child: _getPageView(),
      ),
    );
  }

  Widget _getPageView() {
    return BlocBuilder<TodoCubit,TodoState>(
      builder: (context,state){
        print(state);
        if(state is TodoLoaded){
          return Column(
            children: state.todos.map((e) => Text(e.content )).toList(),
          );
        }else if(state is TodoLoading){
          return _buildLoadingView();
        }else if(state is TodoEmpty){
          return _buildEmptyCartView();
        }else {
          return Container();
        }
      },
    );
  }
  Widget _buildLoadingView() {
    return Center(
      child: CircularProgressIndicator(),
    );
  }
  Widget _buildEmptyCartView() {
    return Center(
      child: Text("Cart is empty!"),
    );
  }
}
